package com.yadang.web.shiro;

import com.yadang.admin.entity.User;
import com.yadang.common.shiro.ShrioUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;

public class ShiroRealm extends AuthorizingRealm {

    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        return null;
    }

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        String username = (String) authenticationToken.getPrincipal();
        String password = "123123";
        User user = new User();
        user.setName(username);
        user.setPassword(ShrioUtils.encript(username, password));
        // param1：用戶对象或者String，param2：数据库中的密码，param3:盐值，param4：getName()
        return new SimpleAuthenticationInfo(user, ShrioUtils.encript(username, password), ByteSource.Util.bytes(username), getName());
    }
}
